<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../_inc/funct.cerBil.php');
require_once('../../_inc/funct.tabDati.php');

$sSql = "SELECT rb.*, rg.descr AS rg_descr, rg.data_rif AS rg_data_rif "
	." FROM rgl_ratbil rb "
	." LEFT JOIN rgl_rating rg ON rg.id_rating = rb.id_rating "
	." WHERE rb.id_rating = ".reqvarInt('id');
$objDB->query($sSql,'selRec');
if (!$acRat = $objDB->fetch('selRec')) {
	backOnErr("rbilIndiciVis - var. id '".reqVar('id')."' mancante o errato - $sSql");
}
$aVar = unsgzu($acRat['str_dati']);

$aDefDati = unsgzu($acRat['str_def_dati']);

$sCmdL = '';
$sCmdL .= "<a class='cmd1' href='rbilIndiciPdf.php?id=".reqVarInt('id')
		."&xsn=".XSN."'>PDF</a>";
$sCmdL .= "&nbsp;&nbsp;&nbsp;";
$sCmdL .= "<a class='cmd1' href='rbilBilRicVis.php?id=".reqvarInt('id')
      		."&xsn=".XSN."'>B. bilancio riclassificato</a>\n";
$sCmdL .= "&nbsp;&nbsp;&nbsp;";
$sCmdL .= "<a class='cmd1' href='rbilRenFinVis.php?id=".reqvarInt('id')
      		."&xsn=".XSN."'>C. rendiconto finanziario</a>\n";
$sCmdL .= "&nbsp;&nbsp;&nbsp;";
$sCmdL .= "<a class='cmd1' href='rbilIndiciVis.php?id=".reqvarInt('id')
      		."&xsn=".XSN."'>D. indici di bilancio</a>\n";


$sCmdR = '';
$sCmdR .= cmdTorna('torna');

testaHtml('', $msgPag, 'ratg');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" align="center" colspan='20'><b>Azienda</b></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.fisc.</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_FISC']?></td>
      <td class="lab1" align="left" colspan='3'>Cod CCIAA</td>
      <td class="txt1" align="left" colspan='7'><?=$aVar['B_COD_CCIAA']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Ragione sociale</td>
      <td class="txt1" align="left" colspan='17'>
      	<a class='txt1' href='../cbil/anagVis.php?id=<?=$aVar['B_ID_AZIENDA']?>&xsn=<?=XSN?>'>
      	<?=$aVar['B_RAG_SOC']?></a></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Sede</td>
      <td class="txt1" align="left" colspan='17'><?=$aVar['B_SEDE']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod.sett.</td>
      <td class="txt1" align="left" colspan='3'><?=$aVar['B_COD_SETT']?></td>
      <td class="txt1" align="left" colspan='14'><?=$aVar['B_DESCR_SETT']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Rating</td>
      <td class="txt1" align="right" colspan='1'>
      	<a class='txt1' href='rglobVis.php?id=<?=$acRat['id_rating']?>&xsn=<?=XSN?>'>
      	&nbsp;<?=$acRat['id_rating']?>&nbsp;</a></td>
      <td class="txt1" align="center" colspan='2'><?=YMD2DMY($acRat['rg_data_rif'])?></td>
      <td class="txt1" align="left" colspan='14'><?=$acRat['rg_descr']?></td>
   </tr>
	<tr>
		<td class='tit1'align='center' colspan='20'><b>BILANCIO</b></td>
	<tr>
<?
$aGeDati = getGerarcDefDati($aDefDati,1);
//dbg_($aGeDati);
//dbg_($aVar);
foreach($aGeDati as $codX => $aX) {
	echoDato($codX,$aX,$aVar,0);
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td> 
   </tr>
</table>
<?
echo "</form>\n";

codaHtml();
exit();


function echoDato($codX, $aX, &$aVar, $liv) {
	global $objSess, $objDB;
	$aCSez = array(''=>'-- non sezione --');
	$aCSez['spatt'] = 'Stato patrimoniale - Attivo';
	$aCSez['sppas'] = 'Stato patrimoniale - Passivo';
	$aCSez['cecon'] = 'Conto economico';
	$aCSez['altro'] = 'Altri dati';
	$aCSez['calco'] = 'Dati calcolati';
	
	if ($aX['attr']['cod_sez']!='') {
		echo "<tr><td class='txt1'align='center' colspan='12'><b>".$aCSez[$aX['attr']['cod_sez']]."</b></td>\n"
			."</td><td class='txt1' align='center' colspan='3'><i>".YMD2DMY($aVar['B_DATA_RIF'])."</i></td>\n"
			."</td><td class='txt1' align='center' colspan='3'><i>".YMD2DMY($aVar['C_DATA_RIF'])."</i></td>\n"
			."</td><td class='txt1' align='center' colspan='2'><i>Variaz.%</i></td>\n<tr>\n";
	}
	$codV = 'B_'.$codX;
	$codP = 'C_'.$codX;
	$numX = '';
	$valX = '';
	switch ($aX['attr']['tipo_valore']) {
		case 'label':
			break;
		case 'euro':
		case 'num':
			$numX = (isset($aVar[$codV])) ? $aVar[$codV] : 0;
			$valX = number_format($numX,2,',','.');
			break;
	}
	$numY = 0;
	$valY = '';
	$diff = '';
	switch ($aX['attr']['tipo_valore']) {
		case 'label':
			break;
		case 'euro':
		case 'num':
			$numY = (isset($aVar[$codP])) ? $aVar[$codP] : 0;
			$valY = number_format($numY,2,',','.');
			$diff = ($numY!=0) ? number_format(($numX-$numY)*100/abs($numY),1,',','.').'%' : '';
			break;
	}			
	echo "<tr><td class='txt1' colspan='12'>".str_repeat("&nbsp;",$liv*8)
		."$codX - ".$aX['attr']['descr']
		."</td><td class='txt1' align='right' colspan='3'>$valX</td>"
		."</td><td class='txt1' align='right' colspan='3'>$valY</td>"
		."</td><td class='txt1' align='right' colspan='2'>$diff</td><tr>\n";
	foreach($aX['figli'] as $codY => $aY)  {
		echoDato($codY, $aY, $aVar, $liv+1);
	}
}
